﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Data.SqlClient;

namespace seminarska_SI
{
    public partial class BlokiranjeAplikacii : Form
    {
        public BlokiranjeAplikacii()
        {
            InitializeComponent();
            ispolni();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string ime = openFileDialog1.FileName;
                FileInfo file = new FileInfo(ime);
                listBox1.Items.Add(file.Name);

                SqlConnection konekcija = new SqlConnection();
                konekcija.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=D:\\seminarska\\seminarska_SI\\Baza.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";

                SqlCommand komanda = new SqlCommand();
                komanda.Connection = konekcija;
                komanda.CommandText = "INSERT INTO KillProcess (KillProcess)" + "VALUES(@KillProcess)";
                komanda.Parameters.AddWithValue("@KillProcess", file.Name);

                try
                {
                    konekcija.Open();
                    komanda.ExecuteNonQuery();
                }

                finally
                {
                    konekcija.Close();

                }


            }

        }
        void ispolni()
        {
            SqlConnection konekcija = new SqlConnection();
            konekcija.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=D:\\seminarska\\seminarska_SI\\Baza.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";

            string sqlString = "SELECT * FROM KillProcess";
            SqlCommand komanda = new SqlCommand(sqlString, konekcija);

            try
            {
                konekcija.Open();
                SqlDataReader citac = komanda.ExecuteReader();
                while (citac.Read())
                {
                    listBox1.Items.Add(citac["KillProcess"].ToString());
                }
                citac.Close();
            }
            catch (Exception err)
            {
            }
            finally
            {
                konekcija.Close();
            }
        }

        private void BlokiranjeAplikacii_Load(object sender, EventArgs e)
        {

        }

        private void CboxChrome_CheckedChanged(object sender, EventArgs e)
        {
            string chrome = "chrome.exe";
            SqlConnection konekcija = new SqlConnection();
            konekcija.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=D:\\seminarska\\seminarska_SI\\Baza.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";

            SqlCommand komanda = new SqlCommand();
            komanda.Connection = konekcija;
            komanda.CommandText = "INSERT INTO KillProcess (KillProcess)" + "VALUES(@KillProcess)";
            komanda.Parameters.AddWithValue("@KillProcess", chrome);

            try
            {
                konekcija.Open();
                komanda.ExecuteNonQuery();
            }

            finally
            {
                konekcija.Close();

            }
        }

        private void cboxOpera_CheckedChanged(object sender, EventArgs e)
        {
            string opera = "opera.exe";
            SqlConnection konekcija = new SqlConnection();
            konekcija.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=D:\\seminarska\\seminarska_SI\\Baza.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";

            SqlCommand komanda = new SqlCommand();
            komanda.Connection = konekcija;
            komanda.CommandText = "INSERT INTO KillProcess (KillProcess)" + "VALUES(@KillProcess)";
            komanda.Parameters.AddWithValue("@KillProcess", opera);

            try
            {
                konekcija.Open();
                komanda.ExecuteNonQuery();
            }

            finally
            {
                konekcija.Close();

            }
        }

        private void cboxMozilla_CheckedChanged(object sender, EventArgs e)
        {
            string firefox = "firefox.exe";
            SqlConnection konekcija = new SqlConnection();
            konekcija.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=D:\\seminarska\\seminarska_SI\\Baza.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";

            SqlCommand komanda = new SqlCommand();
            komanda.Connection = konekcija;
            komanda.CommandText = "INSERT INTO KillProcess (KillProcess)" + "VALUES(@KillProcess)";
            komanda.Parameters.AddWithValue("@KillProcess", firefox);

            try
            {
                konekcija.Open();
                komanda.ExecuteNonQuery();
            }

            finally
            {
                konekcija.Close();

            }
                        
        }

        private void cboxuTorrent_CheckedChanged(object sender, EventArgs e)
        {
            string uTorrent = "uTorrent.exe";
            SqlConnection konekcija = new SqlConnection();
            konekcija.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=D:\\seminarska\\seminarska_SI\\Baza.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";

            SqlCommand komanda = new SqlCommand();
            komanda.Connection = konekcija;
            komanda.CommandText = "INSERT INTO KillProcess (KillProcess)" + "VALUES(@KillProcess)";
            komanda.Parameters.AddWithValue("@KillProcess", uTorrent);

            try
            {
                konekcija.Open();
                komanda.ExecuteNonQuery();
            }

            finally
            {
                konekcija.Close();

            }
                                    
        }
    }
}
